package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.common.AjaxResult;
import com.dongdongshop.common.PageResult;
import com.dongdongshop.common.ResultEnum;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.pojo.TbGoodsES;
import com.dongdongshop.service.TbGoodsService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("goods")
public class GoodsController {
    Logger logger = LoggerFactory.getLogger(GoodsController.class);
    @DubboReference
    private TbGoodsService tbGoodsService;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    /*商品管理页面跳转*/
    @RequestMapping("runGoods")
    public String runSeller(){
        return "admin/goods";
    }
    /*商品管理页面展示*/
    @RequestMapping("selectInfoLimitServer")
    @ResponseBody
    public PageResult<TbGoods> selectInfoLimitServer(TbGoods goods, @RequestParam(value = "pageNumber",defaultValue = "1") Integer pageNumber,
                                                       @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize) {
        PageResult<TbGoods> pageBean = tbGoodsService.selectInfoLimitServer(goods,pageNumber, pageSize);

        return pageBean;
    }
    @RequestMapping("selectInfoLimitServerES")
    @ResponseBody
    public PageResult<TbGoodsES> selectInfoLimitServerES(TbGoods goods, @RequestParam(value = "pageNumber",defaultValue = "1") Integer pageNumber,
                                                     @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize) {
        PageResult<TbGoodsES> pageBean = tbGoodsService.selectInfoLimitServerES(goods,pageNumber, pageSize);

        return pageBean;
    }
    /*商品审核*/
    @RequestMapping("commodityAudit")
    @ResponseBody
    public AjaxResult commodityAudit(@RequestParam("goodIds[]") Long[] goodIds){
        /*把数组转为json格式*/
        String s = JSONObject.toJSONString(goodIds);
        try {
            int i   = tbGoodsService.getCommodityAuditIds(goodIds);
            if (i>0){
                rocketMQTemplate.convertAndSend("tbGoods",s);
                System.out.println("我的收藏MQ!发送成功!");
                return AjaxResult.R(ResultEnum.SUCCESS);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("商品审核失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
    @RequestMapping("commodityFail")
    @ResponseBody
    public AjaxResult commodityFail(@RequestParam("goodIds[]") Long[] goodIds){
        try {
            int i   = tbGoodsService.getCommodityFailIds(goodIds);
            if (i>0){
                return AjaxResult.R(ResultEnum.SUCCESS);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("商品驳回失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
}
